package fem2.observer;

import java.io.IOException;

import fem2.Model;
import fem2.strategies.BucklingStrategy;

public class GidBucklingModeObserver extends GidStructuralObserver {

	private BucklingStrategy bs;

	public GidBucklingModeObserver(Model m, TimeStepObserver o, BucklingStrategy bs,
			String projectDir, String projectName) {
		super(m, o, projectDir, projectName);
		this.bs = bs;
	}

	public GidBucklingModeObserver(Model m, TimeStepObserver o, BucklingStrategy bs,
			String projectDir, String projectName, String meshFileName) {
		super(m, o, projectDir, projectName, meshFileName);
		this.bs = bs;
	}

	@Override
	public void fireStarted() {
	}

	@Override
	public void fireTimeStepStarted() {
	}

	@Override
	public void fireIterationStarted() {
	}

	@Override
	public void fireIterationFinished() {
	}

	@Override
	public void fireTimeStepFinished() {
	}

	@Override
	public void fireFinished() {
		int n = bs.getNumberOfBucklingModes();
		for (int i = 0; i < n; i++) {
			bs.setCurrentTimeStep(i);
			double[] u = bs.getCurrentUHat();
			m.setX(0.0, u);

			double step = i + 1;
			try {
				gidPost.writeMesh(m, step);
				gidPost.writeNodalDisplacementResults(m, step);
			} catch (IOException e) {
				throw new Error("Write failed");
			}
		}
	}

}
